package com.da.lon.wang.xlg.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * <dl>
 * <dt>SharePreference管理类</dt>
 * <dd></dd>
 * </dl>
 */
public class PreferenceUtils {
    private static final String SettingFileName = "xlg_sp";
    private static PreferenceUtils mInstance;
    private SharedPreferences settings;

    public PreferenceUtils(Context context) {
        settings = context.getSharedPreferences(SettingFileName,
                Context.MODE_WORLD_READABLE);
    }

    public static final PreferenceUtils getInstance() {
        if (null == mInstance) {
            throw new IllegalArgumentException(
                    "You must call init() method before call getInstance()");
        }
        return mInstance;
    }

    public static final void init(final Context context) {
        if (null == mInstance) {
            mInstance = new PreferenceUtils(context);
        }
    }

    public SharedPreferences getPreferences() {
        return settings;
    }

    /**
     * <dt>功能：获取对应键的字符串值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getSettingStr(String key, String defaultValue) {
        return settings.getString(key, defaultValue);
    }

    public String getSettingXmlStr(String key, String defaultValue) {
        return settings.getString(key, defaultValue).replace("&amp;", "&");
    }

    /**
     * <dt>功能：获取对应键的整数值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public int getSettingInt(String key, int defaultValue) {
        return settings.getInt(key, defaultValue);
    }

    public Long getSettingLong(String key, long defaultValue) {
        return settings.getLong(key, defaultValue);
    }

    /**
     * <dt>功能：获取对应键的boolean值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public Boolean getSettingBool(String key, boolean defaultValue) {
        return settings.getBoolean(key, defaultValue);
    }

    /**
     * <dt>功能：设置对应键的字符串值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param value
     */
    public void SetSettingString(String key, String value) {
        settings.edit().putString(key, value).commit();
    }

    /**
     * <dt>功能：设置对应键的整数值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param value
     */
    public void SetSettingInt(String key, int value) {
        settings.edit().putInt(key, value).commit();
    }

    public void SetSettingLong(String key, Long value) {
        settings.edit().putLong(key, value).commit();
    }

    /**
     * <dt>功能：设置对应键的boolean值</dt> <dt>作者： danielyip</dt>
     *
     * @param key
     * @param value
     */
    public void SetSettingBoolean(String key, boolean value) {
        settings.edit().putBoolean(key, value).commit();
    }

}
